package com.hundsun.cprs.yyt.service.common.dto;

import java.util.ArrayList;
import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.hundsun.cprs.yyt.service.common.enums.EnumFundBidCode;
import com.hundsun.cprs.yyt.util.DateUtil;
import com.hundsun.hps.request.AtomicRequest;
import com.hundsun.hps.request.CommonRequest;

/**
 * 资金批量请求链式调用包装器
 * @author linrl
 * @date 2018年4月11日
 */
public class FundBatchRequest {
    
    private CommonRequest commonRequest;
    
    /* 附加一些信息 用于记录资金对账流水*/
    private Long goodsId;
    private Long userId;
    
    private FundBatchRequest(String operator,String bizNo,String memo){
        commonRequest = new CommonRequest();
        commonRequest.setVersion("1.0");
        commonRequest.setOutBizNo(bizNo);
        commonRequest.setTransDate(DateUtil.convertDateToString("yyyyMMdd", new Date()));
        commonRequest.setExchangeId("0");
        commonRequest.setOperator(operator);
        commonRequest.setNeedTransaction("y");
        commonRequest.setMemo(memo);
        commonRequest.setAtomicList(new ArrayList<AtomicRequest>());
    }
    
    /**
     * @param operator  操作人
     * @param bizNo     外部业务流水号
     * @param memo      描述
     * @return
     */
    public static FundBatchRequest builder(String operator,String bizNo,String memo){
        return new FundBatchRequest(operator,bizNo,memo);
    }
    
    public FundBatchRequest setBizNo(String bizNo){
        commonRequest.setOutBizNo(bizNo);
        return this;
    }
    
    public String getBizNo(){
        return commonRequest.getOutBizNo();
    }
    
    public FundBatchRequest setMemo(String memo){
        commonRequest.setMemo(memo);
        return this;
    }
    
    public String getMemo(){
        return commonRequest.getMemo();
    }
    
    /**
     * 添加顺序即为资金系统的执行顺序
     * @param fundAccount
     * @param transType
     * @param speTransNo
     * @param amount
     * @return
     */
    public FundBatchRequest addTask(String fundAccount,EnumFundBidCode transType,String speTransNo,long amount){
        return addTask(fundAccount, transType, speTransNo, amount, null);
    }
    
    public FundBatchRequest addTask(String fundAccount,EnumFundBidCode transType,String speTransNo,long amount,String memo){
        AtomicRequest request = new AtomicRequest();
        request.setExecuteNo((commonRequest.getAtomicList().size() + 1) + "");
        request.setSpeTransNo(speTransNo);
        request.setFundAccountOrig(fundAccount);
        request.setSubTransCode(transType.getNoCode());
        request.setTransCode(transType.getTransCode().getCode());
        request.setAmount(amount);
        request.setMemo(memo);
        commonRequest.getAtomicList().add(request);
        return this;
    }
    
    /**
     * 返回批量操作资金任务
     * @return 若未添加任何任务,则返回 null
     */
    public CommonRequest returnFundBatchTasks(){
        return commonRequest.getAtomicList().size() > 0?commonRequest:null;
    }

    public Long getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(Long goodsId) {
        this.goodsId = goodsId;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }
    
    public int size(){
        return commonRequest.getAtomicList().size();
    }
    
    @Override
    public String toString() {
         return JSON.toJSONString(this);
    }
    
   /* public static void main(String[] args) {
        
        CommonRequest request = FundBatchRequest.builder("linrl", "11111111", "测试链式调用")
            .addTask("9999999999", EnumFundBidCode.CFUND_160101, "recordNo", 10)
            .addTask("8888888888", EnumFundBidCode.CFUND_160101, "recordNo", 20)
            .returnFundBatchTasks();
        
        System.out.println(JSONObject.toJSON(request));
    }*/
}
